package com.blueseaapps.utility;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;

import com.blueseaapps.data.QuizOptionsData;
import com.blueseaapps.data.QuizQuestionData;

public class QuestionDataGenerator {
	private static final String LOG_TAG = "QuestionDataGenerator";
	public static void main(String[] args) {
	}

	private static Collection<QuizQuestionData> getQuestions(String selCategory, InputStream stream, Context cxt) throws IOException{
		InputStreamReader rdr = new InputStreamReader(stream);
		BufferedReader buffRdr = new BufferedReader(rdr);
		Collection<QuizQuestionData> questionList = new ArrayList<QuizQuestionData>();
		String line = null;
		while((line = buffRdr.readLine()) != null){
			if(line.trim().startsWith("#")){
				continue;
			}
			StringTokenizer str = new StringTokenizer(line,"|");
			if(str.countTokens() < 10){
				Log.d(LOG_TAG, String.valueOf(str.countTokens()));
				Log.d(LOG_TAG, line);
				continue;
			}

			try{

				QuizQuestionData qd = new QuizQuestionData();
				//			No.
				str.nextToken();
				//			Category
				String category = str.nextToken();
				if(( selCategory!= null ) && (! category.trim().equalsIgnoreCase(selCategory))){
					continue;
				}
				qd.addNewCategory(category);
				//			Category Question No
				str.nextToken();
				//			Image File Name
				String imageName = str.nextToken();
				try{
					InputStream imgStream = cxt.getAssets().open(imageName);
					qd.setImageFileName(imageName);
				}catch(FileNotFoundException fne){
					Log.d(LOG_TAG,"No file found "+imageName);
					continue;
				}
				//			Question
				String question = str.nextToken();
				qd.setQuestion(question);

				//			Option1
				String op1 = str.nextToken();
				//			Option2
				String op2 = str.nextToken();
				//			Option3
				String op3 = str.nextToken();
				//			Option4
				String op4 = str.nextToken();

				QuizOptionsData optionsData = new QuizOptionsData(op1, op2, op3, op4);
				qd.setOptionsData(optionsData);

				//			Answer
				String answer = str.nextToken();
				try{
					int answerInt = Integer.parseInt(answer.trim());
					switch(answerInt){
					case 1:
						answer = op1;
						break;
					case 2:
						answer = op2;
						break;
					case 3:
						answer = op3;
						break;
					case 4:
						answer = op4;
						break;
					default:
						continue;
					}
					qd.setAnswer(answer);
				}catch(NumberFormatException nfe){
					continue;
				}

				questionList.add(qd);
			}catch(NoSuchElementException nse){
				continue;
			}

			}
			Log.d(LOG_TAG, "No of rows: " + String.valueOf(questionList.size()));
			return questionList;
		}

		public static List<QuizQuestionData> populateData(String selCategory, Context cxt) throws IOException {
			String filename="META-INF/PQ.csv";
			AssetManager asstsMgr = cxt.getAssets();
			InputStream fileStream = asstsMgr.open(filename);
			return (List<QuizQuestionData>) getQuestions(selCategory, fileStream, cxt);
		}
	}
